package org.dei.perla.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class PerlaRuntime extends Properties {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7196148393153850902L;

	public static String PERLA_LAST_QUERY_EXECUTED = "PERLA_LAST_QUERY_EXECUTED";
	
	private static PerlaRuntime singleton = null;
	private static String defaultIniFile = null;
	private static String perlaHome = PerlaEnv.getPerlaHome();
	private PerlaRuntime(){
		super();
		if(defaultIniFile == null){
			if((defaultIniFile = PerlaEnv.getPerlaRcName()) == null)
				defaultIniFile = "perlarc.xml";
		}
	}
	
	private void readRuntimeFromFile(String filename){
		try {
			singleton.loadFromXML(new FileInputStream(new File(PerlaRuntime.perlaHome + filename)));
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void init() {
		if(singleton == null){
			singleton = new PerlaRuntime();
			singleton.readRuntimeFromFile(defaultIniFile);
		}
	}
	
	public static PerlaRuntime getDefaultRuntime() {
		PerlaRuntime.init();
		return singleton;
	}




}
